package com.fz.config;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;


/**
 * swagger配置
 * @author fh
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Profile({"dev","uat"})
public class Swagger2Config {

    @Value("${jwt.header}")
    private String tokenHeader;

    //Token Key
    private final static String MER_TOKEN_KEY_DESC = "登录Token";

    //系统管理
    private final static String SYSTEM_PACKAGE = "com.fz.controller.merchant";
    private final static String SYSTEM_GROUP = "系统管理";



    // --- ALL
    @Bean
    public Docket api() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        //用户swagger中token验证
        tokenPar.name(tokenHeader).description(MER_TOKEN_KEY_DESC)
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("全部接口")
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(RequestHandlerSelectors.basePackage("com.fz.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("丰哲科技-商户平台")
                .description("丰哲RESTful API")
                .version("1.0")
                .contact(new Contact("丰哲商户", "null", "null"))
                .build();
    }
    // --- ALL


    // --- 系统管理
    @Bean
    public Docket sysManagerapi() {
        //添加head参数start
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        //用户swagger中token验证
        tokenPar.name(tokenHeader).description(MER_TOKEN_KEY_DESC)
                .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName(SYSTEM_GROUP)
                .apiInfo(sysManagerapiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(RequestHandlerSelectors.basePackage(SYSTEM_PACKAGE))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }

    private ApiInfo sysManagerapiInfo() {
        return new ApiInfoBuilder()
                .title("丰哲科技-商户平台")
                .description("丰哲RESTful API")
                .version("1.0")
                .contact(new Contact("丰哲商户", "null", "null"))
                .build();
    }
    // --- 系统管理

}
